package cn.com.callback;
import groovy.json.*;
import groovy.swing.impl.DefaultAction;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import cn.com.elite.EliteBean;
import java.security.MessageDigest;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.wisdge.dataservice.Result;
import java.util.Map;
import java.util.Date;
import java.util.HashMap;
import java.time.Instant;


import org.json.JSONObject;
import org.json.JSONArray;
import org.json.JSONException;

import cn.com.elite.Util;
import cn.com.kh.getProperty;
import cn.com.kh.KHUtil;

/**
 *
 * @Title: job_HSTLabel_Customers
 * @Description: 华盛证券 标签客户定时任务查询接口
 * @author: chieh
 * @date 2022-12-08 21:50
 *
 */


public class job_HSTLabel_Customers implements CallBackInvoke{
	private static final Log log=LogFactory.getLog(job_HSTLabel_Customers.class);

	public void doCallBack(EliteBean eliteBean,String callBack,Map map,String body) {

		log.debug("-------------------------start job_HSTLabel_Customers 开始调用-----------------");

		String inData = "";
		String outData = "";
		String totalData = "";
		String dataSourceID = "dataSource";


		try {

			List date_formatlist = eliteBean.chaxun_sql("select date_format(curdate(),'%w') AS week,DATE_FORMAT(NOW(), '%d') AS week2 from dual", "dataSource");
			def date_format = date_formatlist.get(0).get("week");//周几
			def date_format2 = date_formatlist.get(0).get("week2");//每月几号

			List hst_Activitylist = eliteBean.chaxun_sql("select tab_activity_name,frequency from hst_Activity where tab_task_type='2' and durationdate<=now() and durationenddate>=now() and tab_activity_state!='3'", "dataSource");//查询批次
			log.debug("hst_Activitylist:" + hst_Activitylist.size);
			if(hst_Activitylist.size() > 0) {
				int arrInt = hst_Activitylist.size();
				for (int i = 0; i < arrInt; i++) {
					String tagname = hst_Activitylist.get(i).get("tab_activity_name");
					String frequency = hst_Activitylist.get(i).get("frequency");

					if(frequency=="1"){//频次每日
						long sign_time=Instant.now().getEpochSecond();
						log.debug("解析信息-" +  "sign_time: " + sign_time);
						String injson="{\"tagname\""+":"+"\""+tagname+"\"}"
						log.debug("injson:" + injson);

						totalData = Querypromocode(eliteBean, injson,dataSourceID);
						//解析SSO返回信息
						//JSONObject jsonObject = new JSONObject(totalData);
						//log.debug("jsonObject:"+jsonObject);
					}
					if(frequency=="2"&&date_format=="1"){//频次每周
						long sign_time=Instant.now().getEpochSecond();
						log.debug("解析信息-" +  "sign_time: " + sign_time);
						String injson="{\"tagname\""+":"+"\""+tagname+"\"}"
						log.debug("injson:" + injson);

						totalData = Querypromocode(eliteBean, injson,dataSourceID);
						//解析SSO返回信息
						//JSONObject jsonObject = new JSONObject(totalData);
						//log.debug("jsonObject:"+jsonObject);
					}
					if(frequency=="3"&&date_format2=="1"){//批次每月
						long sign_time=Instant.now().getEpochSecond();
						log.debug("解析信息-" +  "sign_time: " + sign_time);
						String injson="{\"tagname\""+":"+"\""+tagname+"\"}"
						log.debug("injson:" + injson);

						totalData = Querypromocode(eliteBean, injson,dataSourceID);
						//解析SSO返回信息
						//JSONObject jsonObject = new JSONObject(totalData);
						//log.debug("jsonObject:"+jsonObject);

					}
				}

			}


			outData = "{\"result\":\"1\",\"message\":\"处理成功\",\"data\":[]}";


		} catch (Exception e) {
			log.error("接口处理异常" + e.getMessage());
			// 失败返回
			outData = "{\"result\":\"-1\",\"message\":\"处理失败(" + e.getMessage() + ")\",\"data\":[]}";
		}

		log.debug("outData:"+outData);
	}

	//标签客户查询接口 URI:	/mp-dabs/queryTagMember
	public static String Querypromocode(EliteBean eliteBean , String injson, String dataSourceID) {

		long startTime = System.currentTimeMillis();
		log.debug("处理开始时间:" + startTime);

		long endTime = System.currentTimeMillis();

		String reJson = null;
		String tagname="";
		try {
			String doURL ="";
			//获取配置参数
			String sysurl = getProperty.get("crmurl");//配置本地接口地址

			//华盛 服务顾问列表查询
			doURL = sysurl+"aipHSTLabel_Customers";
			//接口连续调用所谓生成批次号规则yyyyMMddhhmmss 可能重复异常 故此添加2S睡眠
			Thread.sleep(2 * 1000);
			
			// String doURL = "http://172.19.20.35:8080/EliteGeneralHttpTask/GeneralService/aipHSTLabel_Customers";
			log.debug("doURL:" + doURL);
			Map<String, String> headMap = new HashMap<String, String>();
			headMap.put("Content-Type", "application/x-www-form-urlencoded");
			def getResult = Util.doHttpPostUseHead(doURL, injson, "UTF-8", 10000, 0, headMap);
			log.debug("getResult:" + getResult);
		} catch (Exception e) {
			log.debug("ddddddddddd");
			log.error("接口处理异常" + e.getMessage());
			// 失败返回
			reJson = "{\"result\":\"-1\",\"message\":\"接口异常\",\\\"lastid\\\":\"0\"}";
		}
		return reJson;
	}

	// 解析Json值
	public static String GetJV(JSONObject inJson, String getKey) {
		String Rev_Value = null;

		try {
			Rev_Value = String.valueOf(inJson.getBoolean(getKey));
		} catch (Exception e6) {
			try {
				Rev_Value = inJson.getString(getKey);
			} catch (Exception e5) {
				try {
					Rev_Value = String.valueOf(inJson.getInt(getKey));
				} catch (Exception e4) {
					try {
						Rev_Value = String.valueOf(inJson.getLong(getKey));
					} catch (Exception e11) {
						try {
							Rev_Value = String.valueOf(inJson.getDouble(getKey));
						} catch (Exception e1) {
							try {
								Rev_Value = String.valueOf(inJson.getJSONArray(getKey));
							} catch (Exception e2) {
								try {
									Rev_Value = String.valueOf(inJson.getJSONObject(getKey));
								} catch (Exception e3) {
									Rev_Value = "";
								}
							}
						}
					}
				}
			}
		} finally {
			if (Rev_Value == null || Rev_Value.equals("null") || Rev_Value.length() <= 0) {
				Rev_Value = "";
			}
		}
		return Rev_Value;
	}


	// 非空校验isNull
	public static boolean isNull(String value) {

		boolean reFlag = true;
		if (value == "" || value.length() <= 0) {
			reFlag = true;
		} else {
			reFlag = false;
		}
		log.debug(reFlag);
		return reFlag;
	}

	// 非空校验isNullMSG
	public static String isNullMSG(String key, String value) {

		String reResult = "";
		if (value == "" || value.length() <= 0) {
			reResult = key + "必填字段;";
		}
		return reResult;
	}

}